﻿using UnityEngine;
using System.Collections;
using UI;
public class UpdateWindow : Page
{
    public UILabel netTypeLabel;
    public UILabel speedLabel;
    public UILabel loadedNumber;
    public UISlider slider;
    public UISprite pauseBtn;
    public UILabel contentLabel;

    private float timer = 1;

    private AssetUpdateManager updateManager;
    private AssetUpdateManager UpdateManager
    {
        get
        {
            if (updateManager == null)
            {
                updateManager = AssetUpdateManager.Instance;
            }
            return updateManager;
        }
    }
    void Update()
    {
        if (timer >= 1)
        {
            timer = 0;
            Refresh();
        }
        timer += Time.deltaTime;
    }

    public void Refresh()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            netTypeLabel.text = TableTextBlo.GetText("noNet");
        }
        else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
        {
            netTypeLabel.text = TableTextBlo.GetText("moveNet");
        }
        else
        {
            netTypeLabel.text = TableTextBlo.GetText("wifiNet");
        }
        loadedNumber.text = GetMSize(UpdateManager.loadedSize) + "M/" + GetMSize(UpdateManager.totalSize) + "M";
        slider.value = UpdateManager.loadedSize / (float)UpdateManager.totalSize;
        speedLabel.text = UpdateManager.downloadSpeed.ToString("F2") + "kb/s";
        if (UpdateManager.State <= AssetUpdateManager.AssetUpdateState.BIG_DOWNLOAD)
        {
            if (UpdateManager.isPause)
            {
                pauseBtn.spriteName = "UI_ziti_button_jixu";
            }
            else
            {
                pauseBtn.spriteName = "UI_ziti_button_zanting";
            }
            contentLabel.text = LanguageTool.GetTextNoUpdate(1055).Replace("\\r", "\r").Replace("\\n", "\n");
        }
        else
        {
            pauseBtn.spriteName = "UI_ziti_button_queding";
            contentLabel.text = LanguageTool.GetTextNoUpdate(1056).Replace("\\r", "\r").Replace("\\n", "\n");
        }

    }

    //public void OnClose()
    //{
    //    Destroy(gameObject);
    //}

    public void OnContinue()
    {
        if (UpdateManager.State != AssetUpdateManager.AssetUpdateState.BIG_DOWNLOAD)
        {
            Close();
            return;
        }
        //暂停继续
        if (UpdateManager.isPause)
        {
            UpdateManager.userContinue = UserUpdateMethod.CONTINUE;
        }
        else
        {
            UpdateManager.userContinue = UserUpdateMethod.PAUSE;
        }
        Refresh();
    }

    public string GetMSize(long size)
    {
        return (((double)size) / (1 << 20)).ToString("F2");
    }
}
